//
//  QSWebView.h
//  QSSmarkPark-iOS
//
//  Created by 孙俊 on 2018/3/14.
//  Copyright © 2018年 QiSheng. All rights reserved.
//

#import <UIKit/UIKit.h>

#import <WebKit/WebKit.h>
@class QSWebView;

NS_ASSUME_NONNULL_BEGIN

@protocol WKScriptMessageHandler;

@protocol QSWebViewDelegate <NSObject>

@optional

- (void)webViewDidStartLoad:(QSWebView *)webView;
- (void)webViewDidFinishLoad:(QSWebView *)webView;
- (void)webView:(QSWebView *)webView didFailLoadWithError:(NSError * _Nullable)error;
- (void)webView:(QSWebView *)webView didCommitNavigation:(WKNavigation *)navigation;
- (void)webViewDidScroll:(UIScrollView *)scrollView;

@end

@interface QSWebView : UIView

//会转接 WKNavigationDelegate 内部未实现的回调。
@property (weak, nonatomic) id<QSWebViewDelegate> delegate;

///内部使用的webView
@property (nonatomic, readonly) WKWebView *realWebView;

@property (nonatomic, readonly) NSURLRequest* originRequest;

///是否根据视图大小来缩放页面  默认为YES
@property (nonatomic) BOOL scalesPageToFit;

///是否显示进度条 默认是不显示
@property (nonatomic, assign) BOOL showProgressView;

///进度条颜色 默认是蓝色
@property (nonatomic, strong) UIColor *progressViewColor;

///---- WK 的API
@property (nonatomic, readonly) UIScrollView *scrollView;

- (id)loadRequest:(NSURLRequest *)request;
- (id)loadHTMLString:(NSString *)string baseURL:(NSURL * _Nullable)baseURL;

@property (nonatomic, readonly, copy) NSString *title;
@property (nonatomic, copy) void(^titleDidChangeBlock)(NSString *title);

@property (nonatomic, readonly) NSURLRequest *currentRequest;
@property (nonatomic, readonly) NSURL *URL;

@property (nonatomic, readonly, getter=isLoading) BOOL loading;
@property (nonatomic, readonly) BOOL canGoBack;
@property (nonatomic, readonly) BOOL canGoForward;

- (id)goBack;
- (id)goForward;
- (id)reload;
- (id)reloadFromOrigin;
- (void)stopLoading;
- (void)clearCache;

///WKWebView 跟网页进行交互的方法。
- (void)addScriptMessageHandler:(id<WKScriptMessageHandler>)scriptMessageHandler name:(NSString *)name;
///调用JS的方法
- (void)evaluateJavaScript:(NSString *)javaScriptString completionHandler:(void (^ _Nullable)(_Nullable id, NSError * _Nullable error))completionHandler;

@end

NS_ASSUME_NONNULL_END
